package com.nitty.cash.loop.ui.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.CountDownTimer
import android.view.View
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import com.nitty.cash.loop.R
import com.nitty.cash.loop.common.CommonActivity
import com.nitty.cash.loop.databinding.UiSmsUploadBinding
import com.nitty.cash.loop.event.NittyEventData
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.utils.helper.NittyProcessHelper

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/25 18:45
 * @Annotation：SMS上报
 */
class SmsUploadAct: CommonActivity() {
    lateinit var binding: UiSmsUploadBinding
    //等待时长
    private var countdown = 15
    private var countdownTimer: MyCountDownTimer? = null

    override fun getNittyRootView(): View {
        binding = UiSmsUploadBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initNittyView() {
        rootViewBinding.rootLayout.setBackgroundColor(Color.parseColor("#FFFFFFFF"))
        setNittyTitleBarVisibility(View.VISIBLE)
        setNittyBackBtnVisibility(View.INVISIBLE)
        setNittyRightBtnRes(R.mipmap.img_customer_black)
        setNittyTextTitleVisibility(View.GONE)
    }

    override fun initNittyListener() {
        //联系客服
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("SMS Preview Page")
            addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_CUSTOMER,"SMS Preview Page")
        }
    }

    override fun loadNittyData() {
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    class MyCountDownTimer(private val act: SmsUploadAct, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            act.countdown = (millisUntilFinished / 1000).toInt()
            act.showUploadSmsDataTipsText()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            act.showUploadSmsDataComplete()
        }
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showUploadSmsDataTipsText(){
        binding.applySmsDataCountdown.text = "${countdown}s"
        showApplySmsDataStepStatusView()
    }

    /**
     * 倒计时完成
     */
    private fun showUploadSmsDataComplete(){
        showNittyLoading()
        //刷新主流程
        publishEventData(NittyEventData(NittyEventData.TYPE.NITTY_PROCESS_REFRESH))
    }

    /**
     *旋转动画
     */
    private fun getApplySmsDataAnimation(): Animation {
        // 创建一个旋转动画对象，从当前的角度旋转到指定的角度，持续时间为1000毫秒
        val rotateAnimation = RotateAnimation(0F, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        rotateAnimation.setDuration(1000L) // 设置动画持续时间
//        rotateAnimation.fillAfter = false // 动画执行完后保持结束状态
        rotateAnimation.isFillEnabled = true;
        rotateAnimation.fillBefore = false;
        rotateAnimation.setRepeatCount(Animation.INFINITE); // 设置动画重复次数
        return rotateAnimation
    }

    /**
     * 显示状态
     */
    private fun showApplySmsDataStepStatusView(){
        if (countdown > 10){
            binding.imgSmsUploadStatus.setImageResource(R.mipmap.nitty_sms_data_0_icon)

            binding.applySmsDataStep1.setImageResource(R.mipmap.nitty_sms_data_reviewing_icon)
            binding.applySmsDataStep1Text.setTextColor(Color.parseColor("#FF222222"))
            binding.viewLine1.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFBBBBBB")).intoBackground()
//            binding.applySmsDataStep1.startAnimation(getApplySmsDataAnimation())

            binding.applySmsDataStep2.setImageResource(R.mipmap.nitty_sms_data_befor_icon)
            binding.applySmsDataStep2Text.setTextColor(Color.parseColor("#FF222222"))
            binding.viewLine2Top.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFBBBBBB")).intoBackground()
            binding.viewLine2Bottom.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFBBBBBB")).intoBackground()

            binding.applySmsDataStep3.setImageResource(R.mipmap.nitty_sms_data_befor_icon)
            binding.applySmsDataStep3Text.setTextColor(Color.parseColor("#FF222222"))
            binding.viewLine3.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFBBBBBB")).intoBackground()
            return
        }

        if (countdown > 5){
            binding.imgSmsUploadStatus.setImageResource(R.mipmap.nitty_sms_data_5_icon)

            binding.applySmsDataStep1.setImageResource(R.mipmap.nitty_sms_data_complete_icon)
            binding.applySmsDataStep1Text.setTextColor(Color.parseColor("#FF222222"))
            binding.viewLine1.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF4DB673")).intoBackground()
//            binding.applySmsDataStep1.clearAnimation()

            binding.applySmsDataStep2.setImageResource(R.mipmap.nitty_sms_data_reviewing_icon)
            binding.applySmsDataStep2Text.setTextColor(Color.parseColor("#FF222222"))
            binding.viewLine2Top.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF4DB673")).intoBackground()
            binding.viewLine2Bottom.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFBBBBBB")).intoBackground()
//            binding.applySmsDataStep2.startAnimation(getApplySmsDataAnimation())

            binding.applySmsDataStep3.setImageResource(R.mipmap.nitty_sms_data_befor_icon)
            binding.applySmsDataStep3Text.setTextColor(Color.parseColor("#FF222222"))
            binding.viewLine3.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FFBBBBBB")).intoBackground()
            return
        }
        binding.applySmsDataStep1.setImageResource(R.mipmap.nitty_sms_data_complete_icon)
        binding.applySmsDataStep1Text.setTextColor(Color.parseColor("#FF222222"))
        binding.viewLine1.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF4DB673")).intoBackground()
//        binding.applySmsDataStep1.clearAnimation()

        binding.applySmsDataStep2.setImageResource(R.mipmap.nitty_sms_data_complete_icon)
        binding.applySmsDataStep2Text.setTextColor(Color.parseColor("#FF222222"))
        binding.viewLine2Top.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF4DB673")).intoBackground()
        binding.viewLine2Bottom.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF4DB673")).intoBackground()
//        binding.applySmsDataStep2.clearAnimation()

        binding.viewLine3.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF4DB673")).intoBackground()
        if (countdown >= 2){
            binding.imgSmsUploadStatus.setImageResource(R.mipmap.nitty_sms_data_10_icon)
            binding.applySmsDataStep3.setImageResource(R.mipmap.nitty_sms_data_reviewing_icon)
            binding.applySmsDataStep3Text.setTextColor(Color.parseColor("#FF222222"))
//            binding.applySmsDataStep3.startAnimation(getApplySmsDataAnimation())
            return
        }
        binding.imgSmsUploadStatus.setImageResource(R.mipmap.nitty_sms_data_15_icon)
        binding.applySmsDataStep3.setImageResource(R.mipmap.nitty_sms_data_complete_icon)
        binding.applySmsDataStep3Text.setTextColor(Color.parseColor("#FF222222"))
//        binding.applySmsDataStep3.clearAnimation()
    }

    override fun onNittyPublishEvent(event: NittyEventData) {
        super.onNittyPublishEvent(event)
        when(event.eventType){
            NittyEventData.TYPE.NITTY_PROCESS_REFRESHED -> {
                hiddenNittyLoading()
                //需要校验 WhatsApp 或 SMS
                if (NittyProcessHelper.isCheckWhatsApp() || NittyProcessHelper.isCheckSMS()){
//                    CheckAct.start(this)
                } else {
//                    val pName = getProductName()
//                    val pToken = getProductToken()
//                    ActivityOrderDetails.start(this, pName, pToken)
                }
                finish()
            }
            else -> {

            }
        }
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_LOAN_SMS_PREVIEW_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_LOAN_SMS_PREVIEW_END)
        if (countdownTimer != null){
            countdownTimer?.cancel()
            countdownTimer = null
        }
    }
}